//
//  LDY_ArticleTableViewCell.m
//  ProjectOfBStage
//
//  Created by dlios on 15/11/14.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "LDY_ArticleTableViewCell.h"

@implementation LDY_ArticleTableViewCell

- (void)dealloc
{
    [_articleLabel release];
    [_article release];
    [_model release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.articleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.articleLabel];
        [self.articleLabel release];
        
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat height =[[self class] heightOfLabel:self.article andWith:self.contentView.frame.size.width];
    
    self.articleLabel.frame = CGRectMake(0, 0, self.contentView.frame.size.width, height);
    
    self.articleLabel.textColor = [UIColor blackColor];
}



- (void)setModel:(LDY_ArticleModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    self.article = model.html;
    
}




- (void)setArticle:(NSString *)article
{
    if (_article != article) {
        [_article release];
        _article = [article retain];
    }
    
    self.articleLabel.text = article;
    
}


/*
 * 根据要显示的内容计算:label的frame, 并且返回高度值
 */
+ (CGFloat)heightOfLabel:(NSString *)text andWith:(CGFloat)width
{
    //第一步: 设置文本的最大显示范围
    CGSize size = CGSizeMake(width, MAXFLOAT);
    
    //第二步: 设置字体样式
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    
    //
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
